package com.accenture.portal.controller.questionnaire;


import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.accenture.portal.model.User;
import com.accenture.portal.model.questionnaire.Answer;
import com.accenture.portal.model.questionnaire.Option;
import com.accenture.portal.model.questionnaire.Question;
import com.accenture.portal.model.questionnaire.Questionnaire;
import com.accenture.portal.service.UserService;
import com.accenture.portal.service.questionnaire.AnswerService;
import com.accenture.portal.service.questionnaire.OptionService;
import com.accenture.portal.service.questionnaire.QuestionService;
import com.accenture.portal.service.questionnaire.QuestionnaireService;

@Controller
public class QuestionController {

	@Autowired
	QuestionService questionService;
	@Autowired
	OptionService optionService;
	@Autowired
	AnswerService answerService;
	@Autowired
	QuestionnaireService questionnaireService;
	@Autowired
	UserService userService;

	/**后台页面获取问卷的所有题目
	 * @param questionnaireId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getQuestionByQuestionnaire.action")
	public String getQuestionByQuestionnaire(
			@RequestParam("questionnaireId") Integer questionnaireId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		List<Question> questionList = questionService.getQuestionByQuestionnaire(questionnaireId);
		List<Answer> submittedUserIdList = answerService.getSubmittedUserList(questionnaireId);
		Questionnaire questionnaireCurrent = questionnaireService.getQuestionnaireById(questionnaireId);
		
		request.getSession().setAttribute("questionnaireId", questionnaireId);
		model.addAttribute("questionList", questionList);
		model.addAttribute("submittedUserIdList", submittedUserIdList);
		model.addAttribute("questionnaireCurrent", questionnaireCurrent);
		request.getSession().setAttribute("questionnaireCurrent", questionnaireCurrent);
		
		/*JSONArray json = JSONArray.fromObject(questionList);
		String result = json.toString();
		try {
			response.getWriter().write(result);
			response.getWriter().flush();//清空缓冲区，完成写入
		} catch (IOException e) {
			e.printStackTrace();
		}*/
		
		
		/*List<List> questionListOption = new ArrayList<List>();
		for(Integer i=0;i<questionList.size();i++){
			
			List<Option> OptionList = optionService.getOptionByQuestion(questionList.get(i).getQuestionId());
			questionListOption.add(OptionList);
		}
		model.addAttribute("questionListOption", questionListOption);*/
		return "questionnaire/questionnaire";
	}
	
	/**前台页面获取问卷的所有题目
	 * @param questionnaireId
	 * @param ifQuestionnaireSaved
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getQuestionByQuestionnaireFront.action")
	public String getQuestionByQuestionnaireFront(
			@RequestParam("questionnaireId") Integer questionnaireId,
			@RequestParam("ifQuestionnaireSaved") String ifQuestionnaireSaved,//问卷是否有保存过的答案的标记
			HttpServletRequest request,HttpServletResponse response, Model model) {
		List<Question> questionList = questionService.getQuestionByQuestionnaire(questionnaireId);
		Questionnaire questionnaireCurrent = questionnaireService.getQuestionnaireById(questionnaireId);
		Integer optionQuestionCount=0;
		Integer textQuestionCount=0;
		for(Integer i=0;i<questionList.size();i++){
			if(questionList.get(i).getQuestionType()==0){
				textQuestionCount++;//计算本问卷问答题数量
			}else{
				optionQuestionCount++;//计算本问卷选择题数量
			}
		}
		request.getSession().setAttribute("optionQuestionCount", optionQuestionCount);
		request.getSession().setAttribute("textQuestionCount", textQuestionCount);
		request.getSession().setAttribute("questionnaireId", questionnaireId);
		request.getSession().setAttribute("ifQuestionnaireSaved", ifQuestionnaireSaved);
		model.addAttribute("questionList", questionList);
		model.addAttribute("questionnaireCurrent", questionnaireCurrent);
		return "questionnaire/infillingQuestionnaire";
	}
	
	/**后台页面获取指定用户提交的答案
	 * @param questionnaireId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "getQuestionByQuestionnaireBack.action")
	public String getQuestionByQuestionnaireBack(
			@RequestParam("chosedUser") Integer chosedUser,
			@RequestParam("questionnaireId") Integer questionnaireId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		List<Question> questionList = questionService.getQuestionByQuestionnaire(questionnaireId);
//		String ifQuestionnaireSaved="1";
		request.getSession().setAttribute("questionnaireId", questionnaireId);
//		request.getSession().setAttribute("chosedUser", chosedUser);
		User chosedUserModel=userService.findById(chosedUser);
//		request.getSession().setAttribute("ifQuestionnaireSaved", ifQuestionnaireSaved);
		model.addAttribute("questionList", questionList);
		model.addAttribute("chosedUserModel", chosedUserModel);
		return "questionnaire/questionnaireAnswerSubmitted";
	}
	
	
	/**添加问题
	 * @param question
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "addQuestion.action", method = RequestMethod.POST)
	public String addQuestion(@ModelAttribute("question") Question question,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws UnsupportedEncodingException {
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		question.setQuestionQuestionnaire(questionnaireId);
		questionService.addQuestion(question);
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/**跳转添加问题页面
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toAddQuestion.action", method = RequestMethod.GET)
	public String toAddQuestion(HttpServletRequest request,
			HttpServletResponse response, Model model) {
		return "questionnaire/addQuestion";
	}
	
	/**删除问题
	 * @param questionId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "deleteQuestion.action", method = RequestMethod.GET)
	public String deleteQuestion(@RequestParam("questionId") Integer questionId,
			HttpServletRequest request,HttpServletResponse response, Model model) {
		questionService.deleteQuestion(questionId);
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/**编辑问题
	 * @param question
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@RequestMapping(value = "editQuestion.action", method = RequestMethod.POST)
	public String editQuestion(@ModelAttribute Question question,
			HttpServletRequest request, HttpServletResponse response,
			Model model) throws UnsupportedEncodingException {
		question.setQuestionId((Integer)request.getSession().getAttribute("questionId"));
		questionService.editQuestion(question);
		Integer questionnaireId = (Integer)request.getSession().getAttribute("questionnaireId");
		return "redirect:getQuestionByQuestionnaire.action?questionnaireId=" + questionnaireId;
	}
	
	/**跳转编辑问题页面
	 * @param questionId
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEditQuestion.action", method = RequestMethod.GET)
	public String toEditQuestion(@RequestParam("questionId") Integer questionId,
			HttpServletRequest request, HttpServletResponse response,
			Model model) {
		Question question = questionService.getQuestionById(questionId);
		request.getSession().setAttribute("questionId", questionId);
		model.addAttribute("questionLast", question);
		return "questionnaire/editQuestion";
	}
}
